/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.kucukcezve.tabu;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author cemal
 * @mail : cemal.yilmaz@gmail.com
 */
public class TabuCanvas extends Canvas {

    // add cronometer.
    //
    private Game game;
    private Font teamFont;
    private int teamHeight;
    private Font cardHeaderFont;
    private int cardHeaderHeight;
    private Font cardWordsFont;
    private int cardWordHeight;
    private int cardFullHeight;
    private Font menuFont;
    private int menuBarHeight;
    private Rect stopWatchRect;
    private Font stopWatchFont;
    private int stopWatchHeight;
    private String stopWatchString;

    // softkeys.
    private final static int LEFT_SOFTKEY = -6;
    private final static int RIGTH_SOFTKEY = -7;
    private final static int MIDDLE_SOFTKEY = -5;

    public TabuCanvas() {
        setFullScreenMode(true);
        teamFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        teamHeight = teamFont.getHeight() + 3;

        cardHeaderFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
        cardWordsFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

        cardHeaderHeight = cardHeaderFont.getHeight() + 3;
        cardWordHeight = cardWordsFont.getHeight() + 2;

        cardFullHeight = cardHeaderHeight + cardWordHeight * 5;

        menuFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);

        menuBarHeight = menuFont.getHeight();

        stopWatchFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        stopWatchHeight = stopWatchFont.getHeight() + 2;

        init();
    }

    private void init() {
        game = new Game(this);

        stopWatchRect = new Rect(0, teamHeight, getWidth(), stopWatchHeight);
    }

    public void updateStopWatchString(String _time) {
        stopWatchString = _time;
        // we only need to update stopwatch.
        repaint(stopWatchRect.x, stopWatchRect.y, stopWatchRect.w, stopWatchRect.h);
    }

    protected void paint(Graphics g) {
        int w = getWidth();
        int h = getHeight();

        // clear the screen
        g.setColor(0x00FFFFFF);
        g.fillRect(0, 0, w, h);

        // set white
        g.setColor(0);
        // draw team infos

        drawTeams(g);

        // draw cards.
        drawCard(g);

        // draw menu items.
        drawMenuBar(g);

        // draw timer.
        drawStopWatch(g);

    // if only time changed draw only the time.
    }

    private void drawStopWatch(Graphics g) {
        int w = getWidth() / 2;
        g.setColor(0);
        if (stopWatchString != null && stopWatchString.length() > 0) {
            g.drawString(stopWatchString, w, teamHeight, Graphics.HCENTER | Graphics.TOP);
        }
    }

    private void drawTeams(Graphics g) {
        g.setColor(0xAAAAAA);
        g.fillRect(0, 0, getWidth(), teamHeight);
        g.setColor(0x000000);
        g.drawString(game.getTeam(0).name, 0, 2, Graphics.LEFT | Graphics.TOP);
        g.drawString(game.getTeam(1).name, getWidth(), 2, Graphics.RIGHT | Graphics.TOP);
    }

    private void drawCard(Graphics g) {
        int h = getHeight();
        int hm = h / 2;

        h = h - menuBarHeight - teamHeight;

        int hstart = (h - cardFullHeight) / 2;
        int wstart = getWidth() / 6;

        g.setColor(0x000000);
        g.drawRoundRect(wstart, hstart, wstart * 4, cardFullHeight, 10, 10);
        g.drawLine(wstart, hstart + cardHeaderHeight, wstart * 5, hstart + cardHeaderHeight);


        int w = getWidth();

        hstart += cardHeaderHeight;
        g.drawString(game.getCurrentCard().words[0], w / 2, hstart,
                Graphics.BOTTOM | Graphics.HCENTER);

        for (int i = 1; i < 6; i++) {
            hstart += cardWordHeight;
            g.drawString(game.getCurrentCard().words[i], w / 2, hstart, Graphics.BOTTOM | Graphics.HCENTER);
        }
    }

    private void drawMenuBar(Graphics g) {
        g.setColor(0xAAAAAA);
        g.drawRect(0, getHeight() - menuBarHeight, getWidth(), menuBarHeight);
        g.setColor(0x000000);

        CBA menuBar = game.getCBA();

        g.drawString(menuBar.left, 3, getHeight() + 3, Graphics.LEFT | Graphics.BOTTOM);
        g.drawString(menuBar.middle, getWidth() / 2, getHeight() + 3, Graphics.HCENTER | Graphics.BOTTOM);
        if (game.canPass()) {
            g.drawString(menuBar.right, getWidth() - 3, getHeight() + 3, Graphics.RIGHT | Graphics.BOTTOM);
        }
    }

    public void keyPressed(int key) {
        switch (key) {
            case LEFT_SOFTKEY:
                handleLeftSoftKey();
                break;
            case RIGTH_SOFTKEY:
                handleRightSoftKey();
                break;
            case MIDDLE_SOFTKEY:
                handleMiddleSoftKey();
                break;
        }
    }

    private void handleLeftSoftKey() {
        game.handleLeftSoftKey();
        repaint();
    }

    private void handleRightSoftKey() {
        game.handleRightSoftKey();
        repaint();
    }

    private void handleMiddleSoftKey() {
        game.handleMiddleSoftKey();
        repaint();
    }
}
